package com.example.flshtarvel.controller;

import com.example.flshtarvel.service.RouteLikeMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RouteLikeMapController {
    @Autowired
    RouteLikeMapService routeLikeMapService;

    //路线点赞
    @GetMapping("/routeLike/{routeId}")
    public Map<String,Object> routeLike(@PathVariable("routeId")String routeId, HttpSession session){
//    @GetMapping("/routeLike")
//    public Map<String,Object> routeLike(@RequestParam("routeId")String routeId,
//                                        @RequestParam("userId")String userId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = routeLikeMapService.routeLike(routeId,(String)session.getAttribute("userId"));
        if(result==1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //取消点赞
    @GetMapping("/deleteRouteLike/{routeId}")
    public Map<String,Object> deleteRouteLike(@PathVariable("routeId")String routeId, HttpSession session){
//    @GetMapping("/deleteRouteLike")
//    public Map<String,Object> deleteRouteLike(@RequestParam("routeId")String routeId,
//                                                @RequestParam("userId")String userId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = routeLikeMapService.deleteRouteLike(routeId,(String)session.getAttribute("userId"));
        if(result >= 1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //查询点赞    根据路线id 用户id
    @GetMapping("/seleteRouteLike{routeId}")
    public Map<String,Object> selectRouteLikeByRouteIdAndUserId(@PathVariable("routeId")String routeId,
                                                                HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = routeLikeMapService.selectRouteLikeByRouteIdAndUserId(routeId,(String)session.getAttribute("userId"));
        if(result==1){
            System.out.println("1");
        }else{
            System.out.println("0");
        }
        return map;
    }

    //查询点赞数   根据路线id
    @GetMapping("/seleteRouteLikeCount")
    public Map<String,Object> selectRouteLikeByRouteId(String routeId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = routeLikeMapService.selectRouteLikeByRouteId(routeId);
        map.put("msg",1);
        map.put("count",result);
        return map;
    }


}
